﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace Terraria.GameContent
{
	// Token: 0x020003BE RID: 958
	public class PressurePlateHelper
	{
		// Token: 0x06002201 RID: 8705 RVA: 0x0046A7BC File Offset: 0x004689BC
		public static void Update()
		{
			if (!PressurePlateHelper.NeedsFirstUpdate)
			{
				return;
			}
			foreach (Point location in PressurePlateHelper.PressurePlatesPressed.Keys)
			{
				PressurePlateHelper.PokeLocation(location);
			}
			PressurePlateHelper.PressurePlatesPressed.Clear();
			PressurePlateHelper.NeedsFirstUpdate = false;
		}

		// Token: 0x06002202 RID: 8706 RVA: 0x0046A82C File Offset: 0x00468A2C
		public static void Reset()
		{
			PressurePlateHelper.PressurePlatesPressed.Clear();
			for (int i = 0; i < PressurePlateHelper.PlayerLastPosition.Length; i++)
			{
				PressurePlateHelper.PlayerLastPosition[i] = Vector2.Zero;
			}
		}

		// Token: 0x06002203 RID: 8707 RVA: 0x0046A868 File Offset: 0x00468A68
		public static void ResetPlayer(int player)
		{
			foreach (bool[] array in PressurePlateHelper.PressurePlatesPressed.Values)
			{
				array[player] = false;
			}
		}

		// Token: 0x06002204 RID: 8708 RVA: 0x0046A8BC File Offset: 0x00468ABC
		public static void UpdatePlayerPosition(Player player)
		{
			Point point = new Point(1, 1);
			Vector2 vector = point.ToVector2();
			List<Point> tilesIn = Collision.GetTilesIn(PressurePlateHelper.PlayerLastPosition[player.whoAmI] + vector, PressurePlateHelper.PlayerLastPosition[player.whoAmI] + player.Size - vector * 2f);
			List<Point> tilesIn2 = Collision.GetTilesIn(player.TopLeft + vector, player.BottomRight - vector * 2f);
			Rectangle hitbox = player.Hitbox;
			Rectangle hitbox2 = player.Hitbox;
			hitbox.Inflate(-point.X, -point.Y);
			hitbox2.Inflate(-point.X, -point.Y);
			hitbox2.X = (int)PressurePlateHelper.PlayerLastPosition[player.whoAmI].X;
			hitbox2.Y = (int)PressurePlateHelper.PlayerLastPosition[player.whoAmI].Y;
			for (int i = 0; i < tilesIn.Count; i++)
			{
				Point point2 = tilesIn[i];
				Tile tile = Main.tile[point2.X, point2.Y];
				if (tile.active() && tile.type == 428)
				{
					PressurePlateHelper.pressurePlateBounds.X = point2.X * 16;
					PressurePlateHelper.pressurePlateBounds.Y = point2.Y * 16 + 16 - PressurePlateHelper.pressurePlateBounds.Height;
					if (!hitbox.Intersects(PressurePlateHelper.pressurePlateBounds) && !tilesIn2.Contains(point2))
					{
						PressurePlateHelper.MoveAwayFrom(point2, player.whoAmI);
					}
				}
			}
			for (int j = 0; j < tilesIn2.Count; j++)
			{
				Point point3 = tilesIn2[j];
				Tile tile2 = Main.tile[point3.X, point3.Y];
				if (tile2.active() && tile2.type == 428)
				{
					PressurePlateHelper.pressurePlateBounds.X = point3.X * 16;
					PressurePlateHelper.pressurePlateBounds.Y = point3.Y * 16 + 16 - PressurePlateHelper.pressurePlateBounds.Height;
					if (hitbox.Intersects(PressurePlateHelper.pressurePlateBounds) && (!tilesIn.Contains(point3) || !hitbox2.Intersects(PressurePlateHelper.pressurePlateBounds)))
					{
						PressurePlateHelper.MoveInto(point3, player.whoAmI);
					}
				}
			}
			PressurePlateHelper.PlayerLastPosition[player.whoAmI] = player.position;
		}

		// Token: 0x06002205 RID: 8709 RVA: 0x0046AB44 File Offset: 0x00468D44
		public static void DestroyPlate(Point location)
		{
			bool[] array;
			if (PressurePlateHelper.PressurePlatesPressed.TryGetValue(location, out array))
			{
				PressurePlateHelper.PressurePlatesPressed.Remove(location);
				PressurePlateHelper.PokeLocation(location);
			}
		}

		// Token: 0x06002206 RID: 8710 RVA: 0x00017E89 File Offset: 0x00016089
		private static void UpdatePlatePosition(Point location, int player, bool onIt)
		{
			if (onIt)
			{
				PressurePlateHelper.MoveInto(location, player);
				return;
			}
			PressurePlateHelper.MoveAwayFrom(location, player);
		}

		// Token: 0x06002207 RID: 8711 RVA: 0x0046AB74 File Offset: 0x00468D74
		private static void MoveInto(Point location, int player)
		{
			bool[] array;
			if (PressurePlateHelper.PressurePlatesPressed.TryGetValue(location, out array))
			{
				array[player] = true;
				return;
			}
			PressurePlateHelper.PressurePlatesPressed[location] = new bool[255];
			PressurePlateHelper.PressurePlatesPressed[location][player] = true;
			PressurePlateHelper.PokeLocation(location);
		}

		// Token: 0x06002208 RID: 8712 RVA: 0x0046ABC0 File Offset: 0x00468DC0
		private static void MoveAwayFrom(Point location, int player)
		{
			bool[] array;
			if (PressurePlateHelper.PressurePlatesPressed.TryGetValue(location, out array))
			{
				array[player] = false;
				bool flag = false;
				for (int i = 0; i < array.Length; i++)
				{
					if (array[i])
					{
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					PressurePlateHelper.PressurePlatesPressed.Remove(location);
					PressurePlateHelper.PokeLocation(location);
				}
			}
		}

		// Token: 0x06002209 RID: 8713 RVA: 0x0046AC10 File Offset: 0x00468E10
		private static void PokeLocation(Point location)
		{
			if (Main.netMode != 1)
			{
				Wiring.blockPlayerTeleportationForOneIteration = true;
				Wiring.HitSwitch(location.X, location.Y);
				NetMessage.SendData(59, -1, -1, null, location.X, (float)location.Y, 0f, 0f, 0, 0, 0);
			}
		}

		// Token: 0x04003E22 RID: 15906
		public static Dictionary<Point, bool[]> PressurePlatesPressed = new Dictionary<Point, bool[]>();

		// Token: 0x04003E23 RID: 15907
		public static bool NeedsFirstUpdate = false;

		// Token: 0x04003E24 RID: 15908
		private static Vector2[] PlayerLastPosition = new Vector2[255];

		// Token: 0x04003E25 RID: 15909
		private static Rectangle pressurePlateBounds = new Rectangle(0, 0, 16, 10);
	}
}
